from aiogram import types
from aiogram_dialog import DialogManager, StartMode

from src.bot.states import ClientSG
from src.core.exceptions.user import UserTgIdAlreadyExist
from src.database.services.user import UserService


async def user_start(
    message: types.Message | types.CallbackQuery,
    dialog_manager: DialogManager,
    user_service: UserService,
):
    """Команда старта для пользователей"""

    user_id = message.from_user.id
    username = message.from_user.username
    if not username:
        username = message.from_user.first_name
    try:
        await user_service.create_user(
            telegram_id=user_id,
            username=username,
        )
    except UserTgIdAlreadyExist:
        pass

    if isinstance(message, types.CallbackQuery):
        await message.message.delete()

    await dialog_manager.start(ClientSG.start, mode=StartMode.RESET_STACK)
